clear all
set more off
set scheme aspen, permanently

do config
set maxvar 10000

********************************
********************************
****-- Replication .do file for Appendix
********************************
********************************

global pathData = ".../rep_data" /* Add the path to your data folder */
global pathFig = ".../rep_fig" /* Add the path to your figures folder */


********************************
********************************
****------------------------ ESS
********************************
********************************


*-------- Table. A1 -------
cd "$pathData"

use "ESS_selected_waves", replace 

keep if year == 2002 | year == 2014
recode imbleco (0/4 = 1) (5 .c  = 2) (6/10 = 3) (.b .d = .)


cd "$pathFig"

matrix EV = J(9,6,0)
matrix colnames EV = "Pay less 14" "Pay less 02" "Neutral 14" "Neutral 02"  "Pay more 14" "Pay more 02"
matrix rownames EV = "Austria" ///
"Belgium" ///
"Denmark" ///
"Finland" ///
"France" ///
"Germany" ///
"Great Britain" ///
"Sweden" ///
"Portugal"

local i= 1
foreach cnt in AT BE DK FI FR DE GB SE PT {
di "`cnt'"
di 2014
prop imbleco [pw = pspw] if cntry == "`cnt'" & year == 2014
matrix EV[`i',1] = e(b)[1,1] 
matrix EV[`i',3] = e(b)[1,2] 
matrix EV[`i',5] = e(b)[1,3] 
di "`cnt'"
di 2014
prop imbleco [pw = pspw] if cntry == "`cnt'" & year == 2002
matrix EV[`i',2] = e(b)[1,1] 
matrix EV[`i',4] = e(b)[1,2] 
matrix EV[`i',6] = e(b)[1,3] 
local i = `i' + 1
}


esttab  matrix(EV, fmt("2 2")) using tabA1.tex, replace 


*-------- Table. D1 -------
cd "$pathData"

use "ESS_selected_waves", replace 

keep if year == 2008
recode imrccon (0/4 = 1) (5 88  = 2) (6/10 = 3) (77 99 = .)
recode imsclbn ( 7 8 9 = .)

cd "$pathFig"

matrix EV2 = J(5,9,0)
matrix colnames EV2 = "Get more GB" "Neutral GB" "Give more GB" "Get more FR" "Neutral FR" "Give more FR" ///
"Get more DE" "Neutral DE" "Give more DE"
matrix rownames EV2 = "Immediately" ///
"After 1 year" ///
"After worked" ///
"Citizens" ///
"Never" 


prop imsclbn [pw=pspw] if cntry =="GB", over(imrccon)
matrix EV2[1,1] = e(b)[1,1] 
matrix EV2[2,1] = e(b)[1,4] 
matrix EV2[3,1] = e(b)[1,7] 
matrix EV2[4,1] = e(b)[1,10] 
matrix EV2[5,1] = e(b)[1,13] 

matrix EV2[1,2] = e(b)[1,2] 
matrix EV2[2,2] = e(b)[1,5] 
matrix EV2[3,2] = e(b)[1,8] 
matrix EV2[4,2] = e(b)[1,11] 
matrix EV2[5,2] = e(b)[1,14] 

matrix EV2[1,3] = e(b)[1,3] 
matrix EV2[2,3] = e(b)[1,6] 
matrix EV2[3,3] = e(b)[1,9] 
matrix EV2[4,3] = e(b)[1,12] 
matrix EV2[5,3] = e(b)[1,15]


prop imsclbn [pw=pspw] if cntry =="FR", over(imrccon)
matrix EV2[1,4] = e(b)[1,1] 
matrix EV2[2,4] = e(b)[1,4] 
matrix EV2[3,4] = e(b)[1,7] 
matrix EV2[4,4] = e(b)[1,10] 
matrix EV2[5,4] = e(b)[1,13] 

matrix EV2[1,5] = e(b)[1,2] 
matrix EV2[2,5] = e(b)[1,5] 
matrix EV2[3,5] = e(b)[1,8] 
matrix EV2[4,5] = e(b)[1,11] 
matrix EV2[5,5] = e(b)[1,14] 

matrix EV2[1,6] = e(b)[1,3] 
matrix EV2[2,6] = e(b)[1,6] 
matrix EV2[3,6] = e(b)[1,9] 
matrix EV2[4,6] = e(b)[1,12] 
matrix EV2[5,6] = e(b)[1,15]


prop imsclbn [pw=pspw] if cntry =="DE", over(imrccon)
matrix EV2[1,7] = e(b)[1,1] 
matrix EV2[2,7] = e(b)[1,4] 
matrix EV2[3,7] = e(b)[1,7] 
matrix EV2[4,7] = e(b)[1,10] 
matrix EV2[5,7] = e(b)[1,13] 

matrix EV2[1,8] = e(b)[1,2] 
matrix EV2[2,8] = e(b)[1,5] 
matrix EV2[3,8] = e(b)[1,8] 
matrix EV2[4,8] = e(b)[1,11] 
matrix EV2[5,8] = e(b)[1,14] 

matrix EV2[1,9] = e(b)[1,3] 
matrix EV2[2,9] = e(b)[1,6] 
matrix EV2[3,9] = e(b)[1,9] 
matrix EV2[4,9] = e(b)[1,12] 
matrix EV2[5,9] = e(b)[1,15]

 
esttab  matrix(EV2, fmt("2 2")) using tabD1.tex, replace 




*-------- Table. F1 -------
cd "$pathData"

use "ESS_selected_waves", replace 

keep if year == 2014
recode gvtrimg (1/2 = 1) (3  = 2) (4/5 = 3) (7 8 9 = .)


cd "$pathFig"

matrix EV3 = J(9,3,0)
matrix colnames EV3 = "Better" "Same" "Worse" 
matrix rownames EV3 = "Austria" ///
"Belgium" ///
"Denmark" ///
"Finland" ///
"France" ///
"Germany" ///
"Great Britain" ///
"Sweden" ///
"Portugal"


local i= 1
foreach cnt in AT BE DK FI FR DE GB SE PT {
di "`cnt'"
prop gvtrimg [pw = pspw] if cntry == "`cnt'" 
matrix EV3[`i',1] = e(b)[1,1] 
matrix EV3[`i',2] = e(b)[1,2] 
matrix EV3[`i',3] = e(b)[1,3] 
local i = `i' + 1
}


 
esttab  matrix(EV3, fmt("2 2")) using tabE1.tex, replace 



*-------- Figure. F2 -------
cd "$pathData"

use "ESS_selected_waves", replace 

keep if year == 2008
keep if cntry == "GB" | cntry == "DE"

recode imbgeco imueclt imwbcnt  ( 77 88 99 = .)
recode  imdfetn impcntr imsmetn ( 7 8 9 = .)
recode trrprsn schtaut hrshsnt ( 7 8 9 = .)
recode uentrjb bennent prtsick  sblazy sblwcoa sblwlka  ( 7 8 9 = .) 

cd "$pathFig"

local cntryDE "Germany" 
local cntryGB "Great Britain" 

foreach cnt in GB DE {
di "`cnt'"
preserve 
keep if cntry == "`cnt'"


polychoric trrprsn schtaut hrshsnt [pw=pspw]
display r(sum_w)
global N = int(r(sum_w))
matrix r = r(R)
factormat r, n($N) factors(1) 
rotate
predict autho
egen autho_S = std(autho)
replace autho_S = -1 * autho_S


polychoric imbgeco imueclt imwbcnt imdfetn impcntr imsmetn [pw=pspw]
display r(sum_w)
global N = int(r(sum_w))
matrix r = r(R)

factormat r, n($N) factors(1) 
rotate
predict immi
egen immi_S = std(immi) 


polychoric uentrjb bennent prtsick  sblazy sblwcoa sblwlka [pw=pspw]
display r(sum_w)
global N = int(r(sum_w))
matrix r = r(R)

factormat r, n($N) factors(1) 
rotate
predict freeR
egen freeR_S = std(freeR) 
replace freeR_S = -1 * freeR_S


binscatter freeR_S autho_S, line(qfit) n(10) ///
ylabel(-1(0.1)0.6 , angle(horizontal)labsize(vsmall)) ///
xlabel(, angle(horizontal)labsize(vsmall)) ///
xtitle("Liberal to Authoritarian", size(small)) ///
ytitle("Free Riding Beliefs (low to high)", size(small)) ///
title(`cntry`cnt'', size(medsmall))
graph save "`cnt'_1.gph", replace

binscatter freeR_S autho_S, line(qfit) n(10) controls(immi_S) ///
ylabel(-1(0.1)0.6 , angle(horizontal)labsize(vsmall)) ///
xlabel(, angle(horizontal)labsize(vsmall)) ///
xtitle("Liberal to Authoritarian", size(small)) ///
ytitle("", size(small)) ///
title("Controlling for Anti-Immigrant Sent.", size(small))
graph save "`cnt'_2.gph", replace

binscatter freeR_S immi_S, line(qfit) n(10) controls(autho_S) ///
ylabel(-1(0.1)0.6 , angle(horizontal)labsize(vsmall)) ///
xlabel(, angle(horizontal)labsize(vsmall)) ///
xtitle("Pro to Anti-immigrant", size(small)) ///
ytitle("", size(small)) ///
title("Controlling for Lib-Authoritarian", size(small))
graph save "`cnt'_3.gph", replace

restore 
}


graph combine "GB_1.gph" "GB_2.gph" "GB_3.gph" ///
 "DE_1.gph" "DE_2.gph" "DE_3.gph", col(3)
graph save "FigF2.gph", replace



********************************
********************************
****---------------------- BSAS
********************************
********************************

cd "$pathData"

use "bsa1983_2017.dta", replace 


*** recode outcome variables 

recode morewelf welfhelp welffeet sochelp unempjob dolefidl dole ///
(-1 -2 6 7 8 9 = .), gen(morewelf2 welfhelp2 welffeet2 sochelp2 unempjob2 dolefidl2 dole2)

recode househld (99 = .)
*** generate the income measure

do "income_bsas.do"



*-------- Fig. B1 -------


** support for welfare cuts
recode morewelf2 (1 2 = 0) (3 =0) (4 5 = 1), gen(cut_welfare)

** Free riding beliefs: 
* [welfhelp] The welfare state encourages people to stop helping each other
* [welffeet] If welfare benefits weren't so generous, people would learn to stand on their own two feet. • Around here, 
* [unempjob] Most unemployed people could find a job if they really wanted one
* [sochelp] Many people who get social security don't really deserve any help.
* [dolefidl] Most people on the dole are fiddling in one way or another
* [dole] Unemployment benefit are too high and disincentivize job seeking. Note that dole needs to be recoded, for example: recode of dole for 2011 (equals 1 if mention too high and disincentivize job seeking, 0 otherwise )
recode dole2 (2 4 5 = 1) (1 3 4 5 6 7 = 0) 

** recoded so that higher values means more anti-recipient positions 

foreach var in welfhelp2 welffeet2 sochelp2 unempjob2 dolefidl2 {
	replace `var' = (-1 * `var') + 6 
}

polychoric dole2 welfhelp2 welffeet2 sochelp2 unempjob2 dolefidl2  [pw=wtf]

display r(sum_w)
global N = int(r(sum_w))
matrix r = r(R)

factormat r, n($N) factors(3) 
rotate
predict rt

egen free_riding_S = std(rt)

cd "$pathFig"

preserve
collapse cut_welfare [pw = wtf], by(year)
drop if cut_welfare == . 
tw (qfit cut_welfare year) (scatter cut_welfare year, xscale(range(1986(2)2018)) xlabel(1986(4)2018, angle(45)labsize(small)) ///
yscale(range(0.1(0.1)0.5)) ylabel(0.1(0.05)0.5)  xtitle("") ///
ytitle("% Who Support Cuts to Welfare ", size(medsmall)) legend(off) title("Cuts to Welfare", size(med)))
graph save "pro_cuts.gph", replace
restore 


preserve
collapse free_riding_S [pw = wtf], by(year)
drop if free_riding_S == . 
tw (qfit free_riding_S  year) (scatter free_riding_S  year, xscale(range(1986(2)2018)) xlabel(1986(4)2018, angle(45)labsize(small)) ///
yscale(range(-0.5(0.1)0.3)) ylabel(-0.5(0.1)0.3)   xtitle("") ///
ytitle("Free Riding Score", size(medsmall)) legend(off) title("Free Riding Beliefs", size(med)))
graph save "free_riding.gph", replace
restore 


graph combine "pro_cuts.gph" "free_riding.gph", col(2)
graph save "FigB1.gph", replace



*-------- Fig. B3 -------




cd "$pathFig"

preserve
collapse  cut_welfare [pw=wtf], by(hhincqE year)
drop if cut_welfare == .
tw (scatter cut_welfare year if hhincqE == 1) (qfit cut_welfare year if hhincqE == 1, mcol(black) msymbol(smcircle)  ///
 xlabel(1986(4)2018, labsize(small) angle(45))) ///
(scatter cut_welfare year if hhincqE == 5) (qfit cut_welfare year if hhincqE == 5, mcol(red) msymbol(smcircle)  ///
 xlabel(1986(4)2018, labsize(small) angle(45)) ///
 ylabel(0.1(0.1)0.6, labsize(small)) ytitle("% Favor Welfare Cuts",size(medsmall)) ///
title("% Favor Welfare Cuts",size(medsmall)) xtitle("",size(medium)) legend(off))
graph save "WC_inc.gph", replace
restore


preserve
collapse  free_riding_S [pw=wtf], by(hhincqE year)
drop if free_riding_S == .
tw (scatter free_riding_S year if hhincqE == 1) (qfit free_riding_S year if hhincqE == 1, mcol(black) msymbol(smcircle)  ///
 xlabel(1986(4)2018, labsize(small) angle(45))) ///
(scatter free_riding_S year if hhincqE == 5) (qfit free_riding_S year if hhincqE == 5, mcol(red) msymbol(smcircle)  ///
 xlabel(1986(4)2018, labsize(small) angle(45)) ///
yscale(range(-0.5(0.1)0.3)) ylabel(-0.5(0.1)0.3)  ytitle("Free Riding Score",size(medsmall)) ///
title("Free Riding Beliefs",size(medsmall)) xtitle("",size(medium)) legend(off))
graph save "FR_inc.gph", replace
restore


graph combine "WC_inc.gph" "FR_inc.gph"
graph save "FigB3.gph", replace


********************************
********************************
****------------------- BES panel
********************************
********************************

cd "$pathData"

*-------- Fig. B2 -------

use "BES_panelW2.dta", replace

recode immigEconW2 immigCulturalW2 immigrantsWelfareStateW2 (9999 = .)
recode govtHandoutsW2 (99 999 9999= .)
recode welfarePreferenceExpW2 (99 999 9999= .)

recode al1W1_W5 al2W1_W5 al3W1_W5 al5W1_W5 (9999 = .)


keep immigEconW2 immigCulturalW2 immigrantsWelfareStateW2 wt_full_W2  welfarePreferenceExpW2 govtHandoutsW2 ///
al1W1_W5 al2W1_W5 al3W1_W5 al5W1_W5

** immigration index 


polychoric immigEconW2 immigCulturalW2 immigrantsWelfareStateW2  [pw=wt_full_W2]

display r(sum_w)
global N = int(r(sum_w))
matrix r = r(R)

factormat r, n($N) factors(3) 
rotate
predict immi_W2
egen immi_W2_S = std(immi_W2) 
replace immi_W2_S = -1 * immi_W2_S


** other variables 

recode welfarePreferenceExpW2  ( 1 2 = 1) ( 3 4 5 = 0), gen(tooH)


* figure 

cd "$pathFig"

binscatter tooH immi_W2_S, line(qfit) n(10) ///
ylabel(0.1(0.1)0.7 , angle(horizontal)labsize(vsmall)) ///
xlabel(, angle(horizontal)labsize(vsmall)) ///
xtitle("From Pro-Immigration to Anti-Immigration", size(small)) ///
ytitle("% Find Welfare Benef. Too High", size(small)) ///
title("No Controls", size(small))
graph save "besB2_1.gph", replace

binscatter tooH immi_W2_S, control(govtHandoutsW2) line(qfit) n(10) ///
ylabel(0.1(0.1)0.7, angle(horizontal)labsize(vsmall)) ///
xlabel(, angle(horizontal)labsize(vsmall)) ///
xtitle("From Pro-Immigration to Anti-Immigration", size(small)) ///
ytitle("", size(small)) ///
title("Controlling for Free Riding Beliefs", size(small))
graph save "besB2_2.gph", replace

graph combine "besB2_1.gph" "besB2_2.gph"
graph save "FigB2.gph", replace


*-------- Fig. F1 -------

polychoric al1W1_W5 al2W1_W5 al3W1_W5 al5W1_W5   [pw=wt_full_W2]

display r(sum_w)
global N = int(r(sum_w))
matrix r = r(R)

factormat r, n($N) factors(3) 
rotate
predict autho_W2
egen autho_W2_S = std(autho_W2) 
replace autho_W2_S = -1 * autho_W2_S



recode govtHandoutsW2 ( 1 2 3 = 0) ( 4 5 = 1), gen(tooM)


binscatter tooM immi_W2_S, line(qfit) n(20) ///
ylabel(0.1(0.1)0.9 , angle(horizontal)labsize(vsmall)) ///
xlabel(, angle(horizontal)labsize(vsmall)) ///
xtitle("From Pro-Immigration to Anti-Immigration", size(small)) ///
ytitle("% Agree Too Many Like Handouts", size(small)) ///
title("No Controls", size(small))
graph save "besF1_1.gph", replace

binscatter tooM immi_W2_S, control(autho_W2_S) line(qfit) n(20) ///
ylabel(0.1(0.1)0.9, angle(horizontal)labsize(vsmall)) ///
xlabel(, angle(horizontal)labsize(vsmall)) ///
xtitle("From Pro-Immigration to Anti-Immigration", size(small)) ///
ytitle("", size(small)) ///
title("Controlling for Lib-Authoritarian", size(small))
graph save "besF1_2.gph", replace

graph combine "besF1_1.gph" "besF1_2.gph"
graph save "FigF1.gph", replace

********************************
********************************
****------------------- CREDOC
********************************
********************************

*-------- Fig. C1 -------
cd "$pathData"

use "CREDOC79_20", replace

tab effortpp, gen(effort)

rename effort1 Too_Much
rename effort3 Too_Little
rename effort2 About_Right

cd "$pathFig"

collapse About_Right Too_Little Too_Much [pw = pond], by(year)

drop if year < 1990 

tw (scatter Too_Much year, xline(2009)) (scatter About_Right year) (scatter Too_Little year) ///
(lowess Too_Much year) (lowess About_Right year) (lowess Too_Little year), ///
	xtitle("" , size(medsmall))  ///
	ytitle("Percentage" , size(medsmall)) ///
	ylab(0(0.1)0.7,angle(0) labsize(small)) ///
	xlab(1990(5)2020, labsize(small))  title("Gov't Help For the Poor?", size(medium)) ///
	legend(cols(3) order(1 "Too Much" 2 "About Right" 3 "Too Little") position(6))
graph save "FigC1.gph", replace	
	

********************************
********************************
****------------------- DREES
********************************
********************************



*-------- Fig. C2 -------
cd "$pathData"

use "DREES00_15", replace

recode ps1_1 (1 = 1) ( 2 3  = 0) (5 = .), gen(exclu_mal)
recode ps1_2 (1 = 1) ( 2 3  = 0) (5 = .), gen(exclu_ret)
recode ps1_3 (1 = 1) ( 2 3  = 0) (5 = .), gen(exclu_fam)
recode ps1_4 (1 = 1) ( 2 3  = 0) (5 = .), gen(exclu_chom)


alpha exclu_mal exclu_ret exclu_fam exclu_chom, gen(exclu)
sum exclu
gen excluF = (exclu - r(mean)) / r(sd)

egen excluF_M = wtmean(excluF), weight(poid) by(year)


cd "$pathFig"


tw (scatter excluF_M year ) ///
(lpolyci excluF year , deg(3) ///
xsc(range(2000 2014)) xlabel(2000(2)2014, labsize(small) angle(45)) ylabel(-0.2(0.1)0.5, labsize(small)) ytitle("Accessible to Contributors Only (Std. Index)",size(medsmall)) ///
xtitle("") legend(off))
graph save "FigC2.gph", replace	



********************************
********************************
****------------------- ISSP
********************************
********************************



*-------- Fig. B4 -------

cd "$pathData"

use "ISSP_RofG85_16.dta", replace


recode UE ( 1 2 = 1) ( 3 4 = 0) ( 8 9 = .), gen(GovR_UE)
recode red ( 1 2 = 1) ( 3 4 = 0) ( 8 9 = .), gen(GovR_red)

collapse GovR_UE GovR_red [pw=weight], by(year)

tw (line GovR_red GovR_UE year) (scatter GovR_red GovR_UE year, ///
ysc(range(0.4 0.9)) ylabel(0.4(0.1)0.9, labsize(small)) ///
xsc(range(1984 2016)) xlabel(1984(4)2016, labsize(small) angle(45)) ///
ytitle("% Agree Definity/Probably should be Govt's responsibility ..." , size(small)) ///
xtitle("") ///
title("", size(zero)) ///
legend(order(1 "to redistribute" 2 "to provide good stand. of living for Unemp.") size(small)))

cd "$pathFig"
graph save "FigB4.gph", replace	


